package com.example.adms.depart.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.adms.depart.dto.PageParams;
import com.example.adms.depart.entity.Depart;
import com.example.adms.depart.mapper.DepartMapper;
import com.example.adms.depart.service.DepartService;
import com.example.adms.user.entity.User;
import com.example.adms.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DepartServiceImpl extends ServiceImpl<DepartMapper, Depart> implements DepartService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private DepartMapper departMapper;
    /**
     * 根据用户id(从token获取)获取对应部门
     * @param token
     * @return
     */
    @Override
    public Result getDepartById(String token) {
        //通过token获取用户信息
        String s = redisTemplate.opsForValue().get("TOKEN_" + token);
        User user = JSONObject.parseObject(s, User.class);
        Map<String,Object> map = new HashMap<>();
        map.put("uid",user.getId());
        Depart depart = departMapper.selectDepartById(map);
        return Result.success(depart);
    }

    /**
     * 条件分页获取部门列表
     * @return
     */
    @Override
    public Result getDepartListWithCondition(PageParams pageParams) {
        Page<Depart> page = new Page<>(pageParams.getPageNumber(), pageParams.getPageSize());

        LambdaQueryWrapper<Depart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(pageParams.getDepartId()!=null,Depart::getDepartId,pageParams.getDepartId());
        queryWrapper.eq(pageParams.getLeaderId()!=null,Depart::getDepartLeaderId,pageParams.getLeaderId());
        Page<Depart> departPage = this.page(page,queryWrapper);

        List<Depart> departList = departPage.getRecords();
        long total = departPage.getTotal();
        Map<String,Object> map = new HashMap<>();
        map.put("departList",departList);
        map.put("total",total);
        return Result.success(map);
    }

    /**
     * 查询所有部门
     * @return
     */
    @Override
    public List<Depart> getDepartList() {
        List<Depart> list = this.list();
        return list;
    }

    /**
     * 根据userId来查对应的部门
     * @param userId
     * @return
     */
    @Override
    public Depart getDepartByUserId(String userId) {
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        return departMapper.selectDepartById(map);
    }

    /**
     * 添加、修改部门
     * @param depart
     * @return
     */
    @Override
    public Result addOrUpdateDepart(Depart depart) {
        boolean flag = false;
        if (depart.getDepartId() != null){
            //有id，说明是更新
            flag = this.updateById(depart);
        } else {
            //无id，说明是添加
            flag = this.save(depart);
        }
        if (flag){
            return Result.success(null);
        } else {
            return Result.fail(500,"操作失败");
        }
    }
}
